package cn.ds.assets.module.assets.service.dataQualityRuleTemplate;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.dataQualityRuleTemplate.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRuleTemplate.BaseDataQualityRuleTemplateDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.dataQualityRuleTemplate.BaseDataQualityRuleTemplateConvert;
import cn.ds.assets.module.assets.dal.mysql.dataQualityRuleTemplate.BaseDataQualityRuleTemplateMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 数据质量规则模板关联 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseDataQualityRuleTemplateServiceImpl implements BaseDataQualityRuleTemplateService {

    @Resource
    private BaseDataQualityRuleTemplateMapper baseDataQualityRuleTemplateMapper;

    @Override
    public Long createBaseDataQualityRuleTemplate(BaseDataQualityRuleTemplateCreateReqVO createReqVO) {
        // 插入
        BaseDataQualityRuleTemplateDO baseDataQualityRuleTemplate = BaseDataQualityRuleTemplateConvert.INSTANCE.convert(createReqVO);
        baseDataQualityRuleTemplateMapper.insert(baseDataQualityRuleTemplate);
        // 返回
        return baseDataQualityRuleTemplate.getId();
    }

    @Override
    public void updateBaseDataQualityRuleTemplate(BaseDataQualityRuleTemplateUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseDataQualityRuleTemplateExists(updateReqVO.getId());
        // 更新
        BaseDataQualityRuleTemplateDO updateObj = BaseDataQualityRuleTemplateConvert.INSTANCE.convert(updateReqVO);
        baseDataQualityRuleTemplateMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseDataQualityRuleTemplate(Long id) {
        // 校验是否存在
        validateBaseDataQualityRuleTemplateExists(id);
        // 逻辑删除
        baseDataQualityRuleTemplateMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseDataQualityRuleTemplateExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseDataQualityRuleTemplateDO dataDo = baseDataQualityRuleTemplateMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseDataQualityRuleTemplateDO getBaseDataQualityRuleTemplate(Long id) {
        return baseDataQualityRuleTemplateMapper.selectById(id);
    }

    @Override
    public List<BaseDataQualityRuleTemplateDO> getBaseDataQualityRuleTemplateList(Collection<Long> ids) {
        return baseDataQualityRuleTemplateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseDataQualityRuleTemplateDO> getBaseDataQualityRuleTemplatePage(BaseDataQualityRuleTemplatePageReqVO pageReqVO) {
        return baseDataQualityRuleTemplateMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseDataQualityRuleTemplateDO> getBaseDataQualityRuleTemplateList(BaseDataQualityRuleTemplateExportReqVO exportReqVO) {
        return baseDataQualityRuleTemplateMapper.selectList(exportReqVO);
    }

}
